bingo.module('system', function() {
	bingo.service('tableService', function() {
		var layui = "";
		var form = "";
		var table = "";
		var layer = "";
		var curPage = "";
		var searchParams = {};
		var tableId = "tableId";
		var optionCurrent = {};
		var options = {
			elem: "#tableId",
			height: "full-190",
			url: "test",
			page: true,
			text: "<div align='center'>当前无数据</div>",
			limits: [5,10,20,50,100,200],
			cols: [
				[{
						checkbox: true
					},
					{
						title: '操作',
						templet: function(row) {
							return row;
						}
					}
				]
			],
			done: function(res, curr, count) {
				curPage = curr;
				optionCurrent.done();
			}
		};
		return {
			getTableId: function() {
				return tableId;
			},
			getCurPage: function() {
				return curPage;
			},
			init: function(layui, option) {
				form = layui.form;
				table = layui.table;
				layer = layui.layer;
				optionCurrent = option;
				if(option.tableId != undefined && option.tableId != null && option.tableId != '') {
					tableId = option.tableId;
					options.elem = "#" + option.tableId;
				}
				options.cols[0] = option.clos;
				options.url = option.url;
				//初始化表数据
				table.render(options);
				//排序监听
				table.on('sort(tbsort)', function(obj) {
					searchParams.orderField = obj.field;
					searchParams.orderType = obj.type;
					table.reload(tableId, {
						initSort: obj,
						where: searchParams,
						page: {
							curr: 1
						}
					});
				});
				//操作事件初始化
				option.actionFunc(table);
				//查询
				form.on('submit(tbsearch)', function(obj) {
					console.debug(obj.field);
					searchParams = $.extend(searchParams, obj.field);
					table.reload(tableId, {
						where: searchParams,
						page: {
							curr: 1
						}
					});
					return false;
				});
				//新增
				form.on('submit(tbadd)', function(obj) {
					option.add();
					return false;
				});
				//批量删除
				form.on('submit(tbdelete)', function(obj) {
					//判断复选框是否选中
					var checkStatus = table.checkStatus(tableId);
					//判断选中的行数
					if(checkStatus.data.length > 0) {
						layer.confirm('是否要删除这些数据？', function(index) {
							var res = option.delete(checkStatus.data);
							if(res.code === 0) {
								var dataLength = table.cache[tableId].length;
								var dataLength = table.cache[tableId].length;
								if(checkStatus.data.length >= dataLength) {
									curPage = curPage < 2 ? 1 : curPage - 1;
								}
								table.reload(tableId, {
									where: searchParams,
									page: {
										curr: curPage
									}
								});
								layer.close(index);
								layer.msg('删除成功');
							} else {
								layer.msg('处理异常,数据保持不变');
							}
						});
					} else {
						layer.msg('请勾选需要删除的数据');
					}
				});
			},
			//刷新
			tableReload: function(page) {
				if(page != undefined && page != null && page != '') {
					curPage = page;
				}
				table.reload(tableId, {
					where: searchParams,
					page: {
						curr: curPage
					}
				});
			},
			//单条删除
			delete: function(data) {
				layer.confirm('是否要删除此行？', function(index) {
					var datas = new Array();
					datas.push(data);
					var res = optionCurrent.delete(datas);
					if(res.code === 0) {
						var dataLength = table.cache[tableId].length;
						if(datas.length >= dataLength) {
							//处理批量删除时候的最末页数据全删除了请求刷新是上一页数据
							curPage = curPage == 1 ? 1 : curPage - 1;
						}
						table.reload(tableId, {
							where: searchParams,
							page: {
								curr: curPage
							}
						});
						layer.close(index);
						layer.msg('删除成功');
					} else {
						layer.msg('处理异常,数据保持不变');
					}
				});
			}
		};
	});
});